package com.yuanye.gwcit.utils;

import android.media.MediaRecorder;

import java.io.IOException;

public class Recorder {

    private MediaRecorder recorder = null;

    public void startRecording(String fileName) {
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(fileName);

        try {
            recorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

        recorder.start();
    }

    public void stopRecording() {
        recorder.stop();
        recorder.release();
        recorder = null;
    }

    public int getAmplitude() {
        if (recorder != null) {
            return recorder.getMaxAmplitude();
        } else {
            return 0;
        }
    }
}
